package com.wzgl.core.config;




import java.io.File;

import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.core.JFinal;
import com.jfinal.ext.handler.UrlSkipHandler;
import com.jfinal.kit.PathKit;
import com.jfinal.kit.Prop;
import com.jfinal.kit.PropKit;
import com.jfinal.log.Log;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.tx.TxByMethodRegex;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.plugin.druid.DruidStatViewHandler;
import com.jfinal.plugin.ehcache.EhCachePlugin;
import com.jfinal.plugin.redis.RedisPlugin;
import com.jfinal.render.ViewType;
import com.jfinal.template.Engine;
import com.jfinal.weixin.sdk.api.ApiConfigKit;
import com.wzgl.core.CacheConsts;
import com.wzgl.core.cache.JCachePlugin;
import com.wzgl.core.config.plugin.COSClientPlugin;
import com.wzgl.core.config.plugin.QuartzManager;
import com.wzgl.core.config.plugin.RedisPluginWXZH;
import com.wzgl.core.controller.GridController;
import com.wzgl.core.controller.HomeController;
import com.wzgl.core.controller.MagicController;
import com.wzgl.core.controller.ReleaseSetingController;
import com.wzgl.core.controller.SeqCodeController;
import com.wzgl.core.controller.TdicinfoContraller;
import com.wzgl.core.controller.UploadController;
import com.wzgl.core.controller.UserController;
import com.wzgl.core.controller.WebSiteController;
import com.wzgl.core.webmagic.utils.DruidPluginUtils;
import com.wzgl.core.xny.model.TnewextendsXny;
import com.wzgl.core.xny.model.TnewsXny;

public class WglConfig extends JFinalConfig {
	static Log log = Log.getLog(WglConfig.class);
	
	/**
	 * 如果生产环境配置文件存在，则优先加载该配置，否则加载开发环境配置文件
	 * @param pro 生产环境配置文件
	 * @param dev 开发环境配置文件
	 */
	public void loadProp(String pro, String dev) {
		try {
			PropKit.use(pro);
		}
		catch (Exception e) {
			PropKit.use(dev);
		}
	}
	@Override
	public void configConstant(Constants me) {
		// TODO Auto-generated method stub
		// 加载少量必要配置，随后可用PropKit.get(...)获取值
		loadProp("wzgl_config_pro.txt", "wzgl_config.txt");
		me.setDevMode(PropKit.getBoolean("devMode", false));
		me.setEncoding("utf-8");
		me.setViewType(ViewType.JSP);
		//设置上传文件保存的路径
		me.setBaseUploadPath(PathKit.getWebRootPath()+File.separator+"myupload");
		// ApiConfigKit 设为开发模式可以在开发阶段输出请求交互的 xml 与 json 数据
		ApiConfigKit.setDevMode(me.getDevMode());
		
	}

	@Override
	public void configRoute(Routes me) {
		//首页
		me.add("/", HomeController.class,"jsp");
		//字典管理 
		me.add("/tdic", TdicinfoContraller.class,"jsp");
		me.add("/grid", GridController.class,"jsp");
		me.add("/user", UserController.class,"jsp");
		me.add("/magic", MagicController.class,"jsp");
		me.add("/release", ReleaseSetingController.class,"jsp");
		me.add("/website", WebSiteController.class,"jsp");
		//上传文件
	    me.add("/upload", UploadController.class,"jsp");
	    //辅助序列码
	    me.add("/seqCode", SeqCodeController.class,"jsp");
	    
	}
	public EhCachePlugin createEhCachePlugin() {
		String ehcacheDiskStorePath = PathKit.getRootClassPath();
		File pathFile = new File(ehcacheDiskStorePath, "ehcache.xml");

//		Configuration cfg = ConfigurationFactory.parseConfiguration();
//		cfg.addDiskStore(new DiskStoreConfiguration().path(pathFile.getAbsolutePath()));
		return new EhCachePlugin(pathFile.toString());
	}
	@Override
	public void configPlugin(Plugins me) {
		  //扩展云存储对象
		  me.add(new COSClientPlugin());
//		  me.add(createEhCachePlugin());
		//缓存插件
		  JCachePlugin leCachePlugin = new JCachePlugin();
		  me.add(leCachePlugin);
		  
		  DruidPlugin druidPlugin = DruidPluginUtils.createDruidPlugin();
		  me.add(druidPlugin);
		  ActiveRecordPlugin arp =DruidPluginUtils.createRecordPlugin(druidPlugin);
		  arp.setCache(leCachePlugin.getCache());
		  arp.setShowSql(JFinal.me().getConstants().getDevMode());
//		  arp.setTransactionLevel(2);
		  arp.setShowSql(true);
		  me.add(arp);
		
		  //犀牛云model
		  Prop dbProp = PropKit.use("db.properties");
		  String xny_url = "jdbc:mysql://" + dbProp.get("xny_db_host").trim() + ":" 
				  			+ dbProp.get("xny_db_host_port").trim() + "/" + dbProp.get("xny_db_name").trim() + "?" + "useUnicode=true&"
				  			+ "characterEncoding=utf8&" + "zeroDateTimeBehavior=convertToNull";
		  DruidPlugin xnyDruidPlugin = new DruidPlugin(xny_url, dbProp.get("xny_db_user").trim(), dbProp.get("xny_db_password").trim());
		  me.add(xnyDruidPlugin);
		  xnyDruidPlugin.setInitialSize(3).setMaxActive(10);
		  ActiveRecordPlugin xnyArp = new ActiveRecordPlugin("xny",xnyDruidPlugin);
		  xnyArp.setShowSql(true);
		  xnyArp.addMapping("t_news","Id", TnewsXny.class);
		  xnyArp.addMapping("t_newextends", "Id",TnewextendsXny.class);
		  
		  me.add(xnyArp);
		  //犀牛云model
		  
		  //942商城
//		  	String shop_url = "jdbc:mysql://" + dbProp.get("shop_db_host").trim() + ":" 
//		  			+ dbProp.get("shop_db_host_port").trim() + "/" + dbProp.get("shop_db_name").trim() + "?" + "useUnicode=true&"
//		  			+ "characterEncoding=utf8&" + "zeroDateTimeBehavior=convertToNull";
//			DruidPlugin shopDruidPlugin = new DruidPlugin(shop_url, dbProp.get("shop_db_user").trim(), dbProp.get("shop_db_password").trim());
//			me.add(shopDruidPlugin);
//			shopDruidPlugin.setInitialSize(3).setMaxActive(10);
//			ActiveRecordPlugin shopArp = new ActiveRecordPlugin("show",shopDruidPlugin);
//			shopArp.setShowSql(true);
//			shopArp.addMapping("tseqCode","fid", ShopTseqCode.class);
//			me.add(shopArp);
			//942商城
			
		  // ehcahce插件配置
		  me.add(new EhCachePlugin());
		  
		  RedisPlugin wzRedis = new RedisPlugin(CacheConsts.CACHE_MAGIC_GIRD_NAME, "127.0.0.1");
		  me.add(wzRedis);
		  RedisPlugin wzlogRedis = new RedisPlugin(CacheConsts.CACHE_SENDED_LOG_NAME, "127.0.0.1");
		  me.add(wzlogRedis);
		  RedisPlugin mRedis = new RedisPlugin(CacheConsts.CACHE_DATA_MAGIC_NAME, "127.0.0.1");
		  me.add(mRedis);
		  RedisPlugin magicLog = new RedisPlugin(CacheConsts.CACHE_LOG_MAGIC_NAME, "127.0.0.1");
		  me.add(magicLog);
		  RedisPlugin weixin_zh = new RedisPlugin(CacheConsts.CACHE_WEIXIN_ZH_NAME, "127.0.0.1");
		  me.add(weixin_zh);
		  RedisPlugin quartz_name = new RedisPlugin(CacheConsts.CACHE_QUARTZ_NAME, "127.0.0.1");
		  me.add(quartz_name);
		  
		  me.add(new QuartzManager());
	}
	
	@Override
	public void configInterceptor(Interceptors me) {
//		me.add(new RequiredUserIdInterceptor());
		// 添加业务层全局拦截器
		me.add(new TxByMethodRegex("(.*save.*|.*update.*|.*del.*|.*create.*.*insert.*)"));
	}

	@Override
	public void configHandler(Handlers me) {
		DruidStatViewHandler dvh =  new DruidStatViewHandler("/druid");
		me.add(dvh); 
		
		me.add(new UrlSkipHandler(".*/services.*",false));  
	}
	/**
	 * 建议使用 JFinal 手册推荐的方式启动项目
	 * 运行此 main 方法可以启动项目，此main方法可以放置在任意的Class类定义中，不一定要放于此
	 */
	public static void main(String[] args) {
		JFinal.start("src/main/webapp", 8899, "/", 5);//启动配置项
	}
	@Override
	public void configEngine(Engine me) {
		// TODO Auto-generated method stub
		
	}
	public void afterJFinalStart(){
		RedisPluginWXZH rp = new RedisPluginWXZH();
		rp.InitRedisPlugin();
	}
}
